package cn.wolfcode.shop.cloud.mq;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(
    consumerGroup = MQConstants.SECKILL_ORDER_SUCCESS_GROUP,
    topic = MQConstants.SECKILL_ORDER,
    selectorExpression = MQConstants.SECKILL_CREATE_SUCCESS_TAG
)
public class OrderSuccessMQListener implements RocketMQListener<Map> {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void onMessage(Map map) {
        System.out.println("进入到成功对列");
        //在真实开发中下单成功可能有很多其他的业务逻辑
        //发送一个延时消息
        MessageBuilder<Map> builder = MessageBuilder.withPayload(map);
        rocketMQTemplate.syncSend(MQConstants.SECKILL_CREATE_DELAY_DEST, builder.build(), 5000, 16);
    }
}
